import 'package:dio/dio.dart';
import 'package:incidentmanager/common/config/constant.dart';
import 'package:incidentmanager/common/utils/local_storage.dart';

class TokenInterceptors extends InterceptorsWrapper {
  @override
  Future onRequest(RequestOptions options) async {
    String token = await LocalStorage.get(TOKEN);
    if (token != null && token != '') {
      options.headers[TOKEN] = 'Jieguantong $token';
    }
    return super.onRequest(options);
  }

  @override
  Future onResponse(Response response) async {
   var headers = response.headers;
   var tokens = headers[TOKEN];
   if (tokens != null && tokens.length > 0) {
     await LocalStorage.save(TOKEN, tokens[0]);
   }
    return super.onResponse(response);
  }

  @override
  Future onError(DioError err) {
    print("ERROR[${err?.response?.statusCode}] => PATH: ${err?.request?.path}");
    return super.onError(err);
  }
}
